import smtplib
import string
import re
import sys

ss = lambda x: string.strip(x)

def mailer(cfg):
    """
    Sending mail python utility
    Usage: pymail config-file
    """
    # reading pymail P-arameter file
    P = {}
    conf = open(cfg, 'r').read()
    for pline in [x for x in string.split(conf, '\n')]:
        if string.find(pline, '=') and len(pline) > 2:
            if pline[0] <> '#':
                k, v = string.split(pline, '=')
                P[ss(k)] = ss(v)
    #print P
    headers =   "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % \
                (P['FROM'], P['TO'], P['SUBJECT'])
    body = open(P['BODY'], 'r').read()
    message = headers + body
    mailServer = smtplib.SMTP(P['SMTPSERVER'])
    mailServer.sendmail(P['FROM'], P['TO'], message)
    mailServer.quit()

if __name__ == "__main__":
    
    if len(sys.argv) <> 2:
        print mailer.__doc__
    else:
	try:
	    mailer(sys.argv[1])
    	except:
            # stay silent ... no matter what
	    pass
            # or wanna be verbose
            #print "Unexpected error:", sys.exc_info()[0]

    #mailer("~/send_email.cfg")
